package com.yanqu.road.dao.impl.channelcenter.entername;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityGroupMatchDaoImpl extends TempDataObjectDao<ActivityGroupMatch> {


    public static String getActivityGroupMatchSql = "select * from `t_n_activity_group_match` where `activityId`=?";
    public Map<Integer, ActivityGroupMatch> getActivityGroupMatch(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ActivityGroupMatch> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getActivityGroupMatchSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityGroupMatch data =  parse(rs);
                result.put(data.getGroupId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateActivityGroupMatchSql = "update `t_n_activity_group_match` set enemyGroupId=?,zoneIdList=? where activityId=? and groupId=?";
    @Override
    protected boolean updateData(ActivityGroupMatch activityGroupMatch) {
        activityGroupMatch.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, activityGroupMatch.getEnemyGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(activityGroupMatch.getZoneIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, activityGroupMatch.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, activityGroupMatch.getGroupId()));
        if (getDb().executeNoneQuery(updateActivityGroupMatchSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertActivityGroupMatchSql = "insert into `t_n_activity_group_match`(activityId,groupId,enemyGroupId,zoneIdList) values (?,?,?,?)";
    @Override
    protected boolean addData(ActivityGroupMatch activityGroupMatch) {
        activityGroupMatch.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, activityGroupMatch.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, activityGroupMatch.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, activityGroupMatch.getEnemyGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(activityGroupMatch.getZoneIdList())));
        if (getDb().executeNoneQuery(insertActivityGroupMatchSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ActivityGroupMatch parseData(ResultSet rs) throws SQLException {
        ActivityGroupMatch activityGroupMatch = new ActivityGroupMatch();
        activityGroupMatch.setActivityId(rs.getInt("activityId"));
        activityGroupMatch.setGroupId(rs.getInt("groupId"));
        activityGroupMatch.setEnemyGroupId(rs.getInt("enemyGroupId"));
        activityGroupMatch.setZoneIdList(JSON.parseObject(rs.getString("zoneIdList"), new TypeReference<List<Integer>>() {}));

        return activityGroupMatch;
    }
}
